package site.qjdb.config.listener;

import java.io.File;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.commons.io.FileUtils;
import site.qjdb.constant.Info;


public class SessionTimeoutListener implements HttpSessionListener {

	@Override
	public void sessionCreated(HttpSessionEvent se) {}

	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		// 删除session中还保存的缓存路径的文件
		HttpSession session = se.getSession();
		String tempPath = (String)session.getAttribute(Info.TEMP_FILE_PATH);
		if(tempPath != null && tempPath.length() > 0) {
			String[] paths = tempPath.split(";");
			String applicationPath = session.getServletContext().getRealPath("").replaceAll("\\\\", "/");
			if(applicationPath.endsWith("/")) {
				applicationPath = applicationPath.substring(0, applicationPath.length() - 1);
			}
			for(String path : paths) {
				path = applicationPath + path;
				File file = new File(path);
				if(file.exists()) {
					FileUtils.deleteQuietly(new File(path));
				}
			}
		}
	}

}
